﻿/*  
cd /projects/hsieh_project/proj_201809/code_1_data/
qsas data_1_sales_new_ind_sh.sas 8 &

Author: Adarsh Kumar
Objective: Sales share of new industries by MSA
*/


libname hr "/projects/hsieh_project/proj_201809/data/";

%Let dir_out = /projects/hsieh_project/proj_201809/data/;

/*
================================================================================
Load raw data

First read in regular LBD data to get total 1977 employment in each msa1983
*/

%include "/projects/hsieh_project/proj_201809/code_1_data/m_read.sas" /source2;

%m_read(param_dev=1,param_lyear=%bquote(1977), param_czone=0, param_msa1983=1, param_msacz=0, param_drop=1);

data lbd; 
  set lbd;
  if year = 1977;
run;

proc sort data=lbd; by msa1983; run; 

proc means data=lbd noprint; 
  by msa1983; 
  output out=dt_msa83_emp77(drop = _type_ _freq_) sum(worker) = emp_msa83_1977;
run;

data dt_msa83_emp77;
  set dt_msa83_emp77;
  rename msa1983 = msa;
run;

%include "/projects/hsieh_project/proj_201809/code_1_data/m_read_sales.sas" /source2;

%m_read_sales(param_dev=0, param_lyear=%bquote(1977, 2012), param_czone=0, param_msa1983=1, param_msacz=0, param_drop=1);

proc import out=cw_sales datafile="&dir_out./cw_year_sales_v12.dta";
run;

proc sort data=lbd; by ch_ind; run; 
proc sort data=cw_sales; by ch_ind; run;

data cn;
  merge lbd(drop=msa) cw_sales(in=in_cw);
  by ch_ind;
  if in_cw = 1;
  if year_min = 1977;
  rename msa1983=msa;
  rename ch_ind = ind;
  if sales > 0;
run;

/* Create list of industries in each MSA in 1977 */
proc sort data=cn; by year msa ind; run;

data cn_1977;
  set cn;
  if year = 1977;
  by year msa ind; 
  if first.ind;
  keep msa ind;
run;

/* Merge with cn(2012) to isolate new industries */ 
data cn_2012;
  set cn; 
  if year = 2012;
run;

data cn_2012_top;
  set cn; 
  if year = 2012;
  if sales_indf_perc <= 0.1;
run;

proc sort data=cn_2012; by year msa ind; run;
proc sort data=cn_2012_top; by year msa ind; run; 

proc means data=cn_2012 noprint; 
  VAR sales; 
  by year msa ind; 
  output out=cn_2012(drop = _type_ _freq_) sum = sales;
run;

proc means data=cn_2012_top noprint; 
  VAR sales; 
  by year msa ind; 
  output out=cn_2012_top(drop = _type_ _freq_) sum = sales;
run;

proc sort data=cn_1977; by msa ind; run;

DATA cn_2012_new;
    merge cn_2012 cn_1977(in=in_77); 
    by msa ind; 
    if in_77 ~= 1;
    keep year msa ind sales;
RUN;

DATA cn_2012_top_new;
    merge cn_2012_top cn_1977(in=in_77); 
    by msa ind; 
    if in_77 ~= 1;
    keep year msa ind sales;
RUN;

/* Sum sales of new ind in msa */ 
proc sort data=cn_2012_new; by year msa; run; 
proc sort data=cn_2012_top_new; by year msa; run; 

proc means data=cn_2012_new noprint; 
  VAR sales;
  by year msa; 
  output out=cn_2012_new(drop = _type_ _freq_) sum=sales_new_ind;
run;

proc means data=cn_2012_top_new noprint; 
  VAR sales;
  by year msa; 
  output out=cn_2012_top_new(drop = _type_ _freq_) sum=sales_new_ind;
run;


/* Produce total sales in each MSA in 2012*/
proc sort data=cn_2012; by msa; run; 

proc means data=cn_2012 noprint; 
  VAR sales; 
  by msa; 
output out=msa_2012_sales(drop=_type_ _freq_) sum=msa_sales;
run;

proc sort data=msa_2012_sales; by msa; run;
proc sort data=cn_2012_new; by msa; run;
proc sort data=cn_2012_top_new; by msa; run;
proc sort data=dt_msa83_emp77; by msa; run;

data cn_2012_new; 
  merge cn_2012_new msa_2012_sales dt_msa83_emp77; 
  by msa; 
  sales_new_sh = sales_new_ind / msa_sales;
run; 

data cn_2012_top_new; 
  merge cn_2012_top_new msa_2012_sales dt_msa83_emp77; 
  by msa; 
  sales_new_sh = sales_new_ind / msa_sales;
run; 

/* Export */
proc export data=cn_2012_new outfile = "&dir_out./sales_new_ind_share.dta" replace;
run;

proc export data=cn_2012_top_new outfile = "&dir_out./sales_new_ind_top_share.dta" replace;
run;

 





